﻿using MMS.Model.FormatModel;
using MMS.UI.Extension;
using MMS.UI.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.WebPages;

namespace MMS.UI.Areas.Admin.Controllers
{

    public class GoodsInfoController : BaseController
    {
        #region 1.0 加载 列表 视图
        public ActionResult Index()
        {
            return View();
        }
        #endregion

        #region 1.1 加载 列表 数据 
        /// <summary>
        /// 1.2 加载 列表 数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Index(FormCollection form)
        {
            string sort = Request.Form["sort"] == null ? "GInventory" : Request.Form["sort"];
            string order = Request.Form["order"] == null ? "asc" : Request.Form["order"];
            int pageIndex = Request.Form["page"].AsInt();
            int pageSize = Request.Form["rows"].AsInt();
            //1.查询数据集合
            var pageData = OpeCur.GetAsync<PageData<Model.GoodsInfo>>("http://localhost:16824/api/GoodsInfo/GetWherePaged/" + pageIndex + "/" + pageSize+ "/" + sort+ "/" + order);

            //2.转成json格式字符串
            var strJson = OpeCur.ToJson(pageData);
            return Content(strJson);
        }
        #endregion

        #region 2.0 加载 新增 视图 
        /// <summary>
        /// 2.0 加载 新增 视图
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
            ViewBag.Category = OpeCur.GetAsync<List<MMS.Model.Category>>("http://localhost:16824/api/GoodsInfo/GetAllCategory").Select(o => new SelectListItem()
            {
                Text = o.CName,
                Value = o.CId.ToString()
            });
            return View();
        }
        #endregion

        #region 2.1 保存 新增 数据
        /// <summary>
        /// 2.1 保存 新增 数据
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Add(ViewModels.GoodsInfoModel viewModel)
        {
            if (ModelState.IsValid)
            {

                OpeCur.PostAsJsonAsync<MMS.Model.GoodsInfo>("http://localhost:16824/api/GoodsInfo/PostAdd", viewModel.ToModel());
                return OpeCur.AjaxMsgOK();
            }
            else
            {
                return OpeCur.AjaxMsgNOOK("请不要禁用浏览器js~~!");
            }
        }
        #endregion

        #region 3.0 加载 修改 视图 
        /// <summary>
        ///  加载 修改 视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Modify(int id)
        {
            ViewBag.Category = OpeCur.GetAsync<List<MMS.Model.Category>>("http://localhost:16824/api/GoodsInfo/GetAllCategory").Select(o => new SelectListItem()
            {
                Text = o.CName,
                Value = o.CId.ToString()
            });

            //1.查询要修改的 权限 实体对象
            var modifyData = OpeCur.GetAsync<MMS.Model.GoodsInfo>("http://localhost:16824/api/GoodsInfo/GetModify/" + id);
            if (modifyData == null) { throw new Exception("找不到要修改的内容~~~~~~~！"); }


            //3.将 实体对象 转成 视图模型对象 传给 视图
            return View(modifyData.ToViewModel());
        }
        #endregion

        #region 3.1 保存 修改 数据
        [HttpPost]
        /// <summary>
        /// 3.1 保存 修改 数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        public ActionResult Modify(int id, ViewModels.GoodsInfoModel viewmodel)
        {
            if (ModelState.IsValid)
            {
                //1.从url参数中 获取 要修改的 对象的 id
                viewmodel.GId = id;
                //2.修改权限
                OpeCur.PostAsJsonAsync<MMS.Model.GoodsInfo>("http://localhost:16824/api/GoodsInfo/PostModify", viewmodel.ToModel());
                return OpeCur.AjaxMsgOK();
            }
            return OpeCur.AjaxMsgNOOK("请不要禁用浏览器js~~~");
        }
        #endregion

        #region 4.0 删除
        /// <summary>
        ///  删除
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Remove(int id)
        {

            OpeCur.GetApi("http://localhost:16824/api/GoodsInfo/GetRemoveById/" + id);
            return OpeCur.AjaxMsgOK();
        }
        #endregion

        #region 5.0 详情
        /// <summary>
        ///  详情
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Details(int id)
        {

            ViewBag.Category = OpeCur.GetAsync<List<MMS.Model.Category>>("http://localhost:16824/api/GoodsInfo/GetAllCategory").Select(o => new SelectListItem()
            {
                Text = o.CName,
                Value = o.CId.ToString()
            });
            var modifyData = OpeCur.GetAsync<MMS.Model.GoodsInfo>("http://localhost:16824/api/GoodsInfo/GetModify/" + id);
            if (modifyData == null) { throw new Exception("找不到要该的内容~~~~~~~！"); }


            //3.将 实体对象 转成 视图模型对象 传给 视图
            return View(modifyData.ToViewModel());
        }
        #endregion

        #region 6.0 加载 商品销售趋势分析 视图
        /// <summary>
        /// 商品销售趋势分析
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Trend(int id)
        {
          var list = OpeCur.GetAsync<List<class_Trend>>("http://localhost:16824/api/GoodsInfo/GetTrend/" + id + "/year/2000-01-01/2017-01-01");
          ViewBag.Data = OpeCur.ToJson(list);
            //3.将 实体对象 转成 视图模型对象 传给 视图
            return View();
        }
        #endregion

        #region 6.1 加载 商品销售趋势分析 数据
        /// <summary>
        /// 商品销售趋势分析
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Trend(int id, FormCollection form)
        {
            string datepart = Request.Form["datepart"];
            string startdate = Request.Form["startdate"];
            string enddate = Request.Form["enddate"];
            startdate = DateTime.ParseExact(startdate, "yyyy-MM-dd HH:mm:ss", null).ToString("yyyyMMddHHmmss");
            enddate = DateTime.ParseExact(enddate, "yyyy-MM-dd HH:mm:ss", null).ToString("yyyyMMddHHmmss");

            var Data = OpeCur.GetAsync<List<class_Trend>>("http://localhost:16824/api/GoodsInfo/GetTrend/" + id + "/" + datepart + "/" + startdate + "/" + enddate);

            var strJson = OpeCur.ToJson(Data);
            return Content(strJson);
          
        }
        #endregion

    }
}
